from llama_index.core.agent.workflow import FunctionAgent, AgentStream
from llama_index.tools.tavily_research import TavilyToolSpec
import os

from llamaIndex.CommonClient import llm
#网络搜索的agent
os.environ["TAVILY_API_KEY"] = "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"

tavily_tool = TavilyToolSpec(api_key=os.getenv("TAVILY_API_KEY"))

workflow = FunctionAgent(
    tools=tavily_tool.to_tool_list(),
    llm=llm,
    system_prompt="You're a helpful assistant that can search the web for information.",
)

async def main():
    handler = workflow.run(user_msg="What's the weather like in San Francisco?")

    async for event in handler.stream_events():
        if isinstance(event, AgentStream):
            print(event.delta, end="", flush=True)




if __name__ == "__main__":
    import asyncio
    asyncio.run(main())